package com.pearl.actuator.demo;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetAddress;

/**
 * @author TangDan
 * @version 1.0
 * @since 2023/5/22
 */
@Component
public class IpHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        // 查看某个IP是否在线
        boolean status = false;
        try {
            status = InetAddress.getByName("127.0.0.1").isReachable(3000);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (status) {
            // 在线，返回UP状态
            return Health.status("fatal")
                    .withDetail("ip", "127.0.0.1").build();
/*            return Health.up()
                    .withDetail("ip", "127.0.0.1").build();*/
        }
        // 不在线，返回DOWN状态
        return Health.down()
                .withDetail("ip", "127.0.0.1").build();
    }
}
